<?php


namespace app\api\console;


use app\api\service\OrderService;
use app\api\service\PharmacyService;
use app\api\service\WechatInfoService;
use app\common\wechat\TemplateMessage;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class AcceptOrderTimeoutRemind extends Command
{
    protected function configure()
    {
        $this
            // 命令的名字（"think" 后面的部分）
            ->setName('app:accept-timeout-remind')

            // 运行 "php think list" 时的简短描述
            ->setDescription('仓储点超时没接单...')

            // 运行命令时使用 "--help" 选项时的完整命令描述
            ->setHelp("仓储点超时没接单....")
        ;
    }

    protected function execute(Input $input, Output $output)
    {
        $appId = \config('wechat.appid');
        $pharmacyService = new OrderService();

        $remindData = $pharmacyService->fetchTimeoutToAcceptOrder();

        if(empty( $remindData)){
            return false;
        }
        $pharmacyService = new PharmacyService();
        $templatePushMessage = new TemplateMessage();

        $wechatInfoService = new WechatInfoService();

        foreach ($remindData as $pharmacy){
            try{

                $pharmacyId = $pharmacy['pharmacy_id'];

                //根据药店的id查询对应的openid 通过公众号推送

                $info = $pharmacyService->getPharmacyInfo($pharmacyId);

                $pharmacyMobile = $info['pharmacy_mobile_phone'];

                $openid = $pharmacyService->getOpenidByMobile($pharmacyMobile);

                //添加订单号
                $remark = "请点击详情进入订单管理里面接收订单完成拣货";
                $templateData['touser'] = $openid;
                $templateData['template_id'] = 'rebYvkDafmrIZ8WxUjvPKyJzF-VczSaBzr3cZsao7yw';
                $templateData['url'] = 'http://weixin.qq.com/download';
                $templateData['data'] = ['first'=>['value'=>'您有新超时未处理的订单'],
                    "keyword1"=>['value'=>'需要点击详情去接收平台派发的订单！'],
                    "keyword2"=>['value'=>date('Y-m-d H:i:s', time()) ],

                    "remark"=>['value'=>$remark]
                ];

                $accessToken = $wechatInfoService->getAccessToken($appId);

                $res = $templatePushMessage->pushTemplateMessage($templateData, $accessToken);

            }catch (\Exception $e){
                var_dump($e);
            }
        }
    }

}